/*
 * \brief  Client-side parent interface
 * \author Norman Feske
 * \date   2006-05-10
 */

/*
 * Copyright (C) 2006-2009 Norman Feske
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__PARENT__CLIENT_H_
#define _INCLUDE__PARENT__CLIENT_H_

#include <parent/capability.h>
#include <base/ipc.h>
#include <base/lock.h>

namespace Genode {

	class Parent_client : public Parent
	{
		private:

			Msgbuf<MSGBUF_SIZE> _snd_msg, _rcv_msg;
			Ipc_client          _ipc_client;
			Lock                _lock;

		public:

			/**
			 * Constructor
			 */
			explicit Parent_client(Parent_capability parent)
			: _ipc_client(parent, &_snd_msg, &_rcv_msg) { }


			/**********************
			 ** Parent interface **
			 **********************/

			void exit(int exit_value)
			{
				Lock::Guard lock_guard(_lock);
				_ipc_client << EXIT << exit_value << IPC_CALL;
			}

			int announce(const char *service_name,
			             Root_capability service_root)
			{
				Lock::Guard lock_guard(_lock);
				return _ipc_client << ANNOUNCE << Buffer(service_name)
				                               << service_root
				                               << IPC_CALL;
			}

			Session_capability session(const char *service_name, const char *args)
			{
				Lock::Guard lock_guard(_lock);
				Session_capability session;
				switch (_ipc_client << SESSION << Buffer(service_name)
				                    << Buffer(args)
				                    << IPC_CALL
				                    >> session) {

				/* translate error codes to exception types */
				case -1: throw Quota_exceeded();
				case -2: throw Service_denied();
				default: break;
				};
				return session;
			}

			void transfer_quota(Session_capability to_session, const char *amount)
			{
				Lock::Guard lock_guard(_lock);
				switch (_ipc_client << TRANSFER_QUOTA << to_session
				                    << Buffer(amount)
				                    << IPC_CALL) {

				/* translate error code to exception type */
				case -1: throw Quota_exceeded();
				default: break;
				}
			}

			void close(Session_capability session)
			{
				Lock::Guard lock_guard(_lock);
				_ipc_client << CLOSE << session << IPC_CALL;
			}
	};
}

#endif /* _INCLUDE__PARENT__CLIENT_H_ */
